/**
 * Copyright (C) 2018-2019
 * All rights reserved, Designed By www.joolun.com
 * 注意：
 * 本软件为www.joolun.com开发研制，未经购买不得使用
 * 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 * 一经发现盗用、分享等行为，将追究法律责任，后果自负
 */
package com.joolun.cloud.weixin.admin.controller;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.joolun.cloud.weixin.admin.config.open.WxOpenConfiguration;
import com.joolun.cloud.weixin.admin.mapper.WxAppMapper;
import com.joolun.cloud.weixin.common.constant.ConfigConstant;
import com.joolun.cloud.weixin.common.constant.WxReturnCode;
import com.joolun.cloud.weixin.common.entity.WxApp;
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.security.annotation.Inner;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.open.bean.result.WxOpenAuthorizerInfoResult;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 开发平台API
 * @author JL
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/open/api")
public class WxOpenApiController {

	private final WxAppMapper wxAppMapper;

	/**
	 * getAuthorizerInfo
	 * @param appId
	 * @return
	 */
    @GetMapping("/authorizer-info/{appId}")
	@PreAuthorize("@pms.hasPermission('wxmp_wxapp_index')")
    public R getAuthorizerInfo(@PathVariable("appId") String appId){
        try {
            WxOpenAuthorizerInfoResult wxOpenAuthorizerInfoResult = WxOpenConfiguration.getOpenService().getWxOpenComponentService().getAuthorizerInfo(appId);
//			WxOpenAuthorizerInfo wxOpenAuthorizerInfo = wxOpenAuthorizerInfoResult.getAuthorizerInfo();
			return R.ok(wxOpenAuthorizerInfoResult);
        } catch (WxErrorException e) {
			e.printStackTrace();
			log.error("getAuthorizerInfo：", e);
			return WxReturnCode.wxErrorExceptionHandler(e);
        }
    }


	/**
	 * 微信登录传入code
	 * <p>
	 * 通过code 调用qq 获取唯一标识
	 *
	 * @param code
	 * @return
	 */
	@GetMapping("/openid/{code}")
	@Inner
	public R identify(@PathVariable("code") String code) {
		WxApp condition = new WxApp();
		condition.setAppType(ConfigConstant.WX_APP_TYPE_2);
		WxApp wxApp = wxAppMapper.selectOne(new QueryWrapper<>(condition));

		String url = String.format(SecurityConstants.WX_AUTHORIZATION_CODE_URL
				, wxApp.getId(), wxApp.getSecret(), code);
		String result = HttpUtil.get(url);
		log.debug("微信响应报文:{}", result);

		try{
			Object obj = JSONUtil.parseObj(result).get("openid");
			return R.ok(obj);
		}catch (Exception e){
			return R.failed("获取openid失败");
		}


	}

}
